package com.xyf.sys.service.impl;

import com.xyf.sys.domain.Delivery;
import com.xyf.sys.domain.UserLogistic;
import com.xyf.sys.mapper.UserLogisticMapper;
import com.xyf.sys.service.IDeliveryService;
import com.xyf.sys.service.IUserLogisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/2/241652
 * @Description
 */
@Service
public class UserLogisticServiceImpl implements IUserLogisticService {

    @Autowired
    private UserLogisticMapper logisticMapper;
    @Autowired
    private IDeliveryService deliveryService;

    @Override
    public List<UserLogistic> getList(String openid) {
        return logisticMapper.selectUserLogisticList(openid);
    }

    @Override
    public List<UserLogistic> getList(Long deptId) {
        return logisticMapper.selectUserLogisticListByDeptId(deptId);
    }

    @Override
    public UserLogistic save(UserLogistic userLogistic) {
        if (userLogistic.getId() == null){
            Long dictId = userLogistic.getDictId();
            Delivery delivery = deliveryService.get(dictId);
            userLogistic.setDeliveryName(delivery.getDeliveryName());
            userLogistic.setBizId(delivery.getBizid());
            userLogistic.setPassword(delivery.getPassword());
            userLogistic.setStatus("0");
            userLogistic.setDelFlag("0");
            userLogistic.setCreateTime(new Date());
            logisticMapper.insert(userLogistic);
            return userLogistic;
        }
        return null;
    }

    @Override
    public int delete(String openid) {
        logisticMapper.deleteByUserId(openid);
        return 0;
    }

    @Override
    public int delete(Long deptId) {
        logisticMapper.deleteByDeptId(deptId);
        return 0;
    }

    @Override
    public int remove(Long logisticsId) {
        UserLogistic userLogistic = logisticMapper.selectByPrimaryKey(logisticsId);
        if (userLogistic != null){
            userLogistic.setStatus("1");
            userLogistic.setDelFlag("1");
            userLogistic.setUpdateTime(new Date());
            userLogistic.setLastUpdateTime(new Date());
            return logisticMapper.updateByPrimaryKey(userLogistic);
        }
        return 0;
    }
}
